/**
 * https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/
 * 删除有序数组的重复项
 * @param nums 
 */
const removeDuplicates = (nums: number[]): number => {
    const len = nums.length;

    // 小于2的, 不需要删除
    if (len < 2) return len;
    let slow = 0,
        fast = 1;

    // 直到快指针越界
    while (fast < len) {
        if (nums[fast] !== nums[slow]) {
            // 不相等
            nums[++slow] = nums[fast]
        }
        fast++
    }

    return slow + 1;
};
removeDuplicates([1,1,1, 2, 3, 3, 4, 4, 5, 5])

const removeDuplicates3 = (nums: number[]): number => {
    const len = nums.length;

    // 小于2的, 不需要删除
    if (len < 2) return len;

    // 保存重复的项
    let v = nums[0],
        newArr = [v]
    for (let i = 1; i < len; i++) {
        if (v === nums[i]) {
            continue
        } else {
            // 不一样, 更新v
            v = nums[i]
            newArr.push(v);
        }
    }

    // 清空nums
    nums.length = 0;
    const l = newArr.length;
    for (let i = 0; i < l; i++) {
        nums[i] = newArr[i]
    }
    return l;
};

const removeDuplicates2 = (nums: number[]): number => {
    const len = nums.length;

    // 小于2的, 不需要删除
    if (len < 2) return len;

    // 保存重复的项
    let v = nums[0]
    for (let i = 1; i < len;) {
        if (v === nums[i]) {
            nums.splice(i, 1)
        } else {
            // 不一样, 更新v
            v = nums[i]
            i++
        }
    }
    return nums.length;
};